package deibay.server.backend.core;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.logging.Level;

import deibay.DeibayLogger;
import deibay.server.events.Event;

public class ProcessEvents implements Runnable {

	private Event eventToProcess = null;
	private HashMap<Class, LinkedList<EventConsumer>> listeners = null;
	
	public ProcessEvents(Event event, HashMap<Class, LinkedList<EventConsumer>> listeners)
	{
		this.eventToProcess = event;
		this.listeners = listeners;
	}
	
	public void run() {
		
		LinkedList consumers = null;
		
		synchronized(listeners)
		{
			consumers = listeners.get(eventToProcess.getClass());
		}
		
		if(consumers == null)
			return;
		
		for(Iterator i = consumers.iterator(); i.hasNext();)
		{
			final EventConsumer consumer = (EventConsumer)i.next();
			DeibayLogger.getInstance().getLog().log(Level.WARNING, "O evento "+eventToProcess.toString()+" foi enviado para o consumidor "+consumer.getClass().getCanonicalName()+".\n");
			consumer.handleEvent(eventToProcess);
		}
	}

}
